/**
 * Copyright 2024 Inc.
 **/
package com.myz.springclond2021.utils;

import java.util.Map;
import java.util.Properties;

/**
 * @author maoyz0621 on 2024/3/26
 * @version v1.0
 */
public class MapToPropertiesConverter {

    public static Properties convert(Map<String, Object> map) {
        Properties properties = new Properties();
        convertToProperties(map, properties, "");
        return properties;
    }

    private static void convertToProperties(Map<String, Object> map, Properties properties, String prefix) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = (prefix.isEmpty() ? "" : prefix + ".") + entry.getKey();
            Object value = entry.getValue();
            if (value instanceof Map) {
                // 递归处理嵌套Map
                convertToProperties((Map<String, Object>) value, properties, key);
            } else {
                // 添加到Properties对象
                properties.put(key, value.toString());
            }
        }
    }
}